/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Interfaces;

import BD.Conector;
import Clases.Licencia;
import Clases.Titular;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import javax.swing.JOptionPane;
import Logica.Logica_titular;
import Logica.Logica_Licencia;
import BD.ABM_Licencia;


public class EmitirLicencia extends javax.swing.JFrame {
    
    //Dato ingresado por pantalla.
    
    private Integer documentoTitular; //DNI que se ingresa en pantalla para posterior validación y parsing
    boolean existeTitular=false;
    
    //Nombre del operador del sistema al momento de la emisión de la licencia.
    
    private String nombreOperador="";
    
    //Datos del titular. Traer desde la tabla titular en la BD.
    
    private String nombreTitular; //Nombre titular. Se corresponde con uno de la BD
    private String apellidoTitular; //Idem anterior.
    private String grupoSanguineoTitular;
    private String factorRHTitular;
    private Date fechaNacimientoTitular;
    private String observacionesTitular;
    private String licenciaElegida;
    
    // Clases donde se cargan los datos obtenidos desde la BD para
    // informar al usuario y validar la licencia que se quiere emitir.
    
    private Titular titularValidado=new Titular();
    private Licencia[] licenciasValidadas={new Licencia(), new Licencia(), new Licencia(), new Licencia(), new Licencia(), new Licencia()};
    
    // Almacenamos aquí la edad del titular para validar en base a las licencias.
    private Integer edadTitular;
    
    /**
     * Creates new form EmitirLicencia
     */
    public EmitirLicencia() {
        
        //Declaramos dos instancias sin inicializar. 
        //Cargamos un titular con el constructor por defecto.
        //Cargamos seis licencias con el constructor por defecto.
        
        initComponents();
        initInstances(titularValidado, licenciasValidadas);
    }
    
    private void initInstances(Titular titularValidado, Licencia[] licenciasValidadas){
        
        //Inicializamos el campo DNI con la cadena vacía.
        textFieldDocumentoTitular.setText("");
    }
    
    private void EmitirNuevaLicencia(Titular titularValidado){
        
        Licencia nuevaLicencia=new Licencia();
        boolean noPoseeLicencia=true;
        Date fechaCaducidadLicencia;
        
        for(int i=0; i<licenciasValidadas.length; i++){
            if(licenciaElegida.equals(licenciasValidadas[i].getTipoLicencia()))
                noPoseeLicencia=false;
        }
            
        if(noPoseeLicencia){
            
            boolean licenciaEmitida=false;
            
            nuevaLicencia.setIDTitularAsociado(documentoTitular);
            nuevaLicencia.setFechaEmision(new Date());
            nuevaLicencia.setNombreOperador(nombreOperador);
            nuevaLicencia.setTipoLicencia(licenciaElegida);
        
            fechaCaducidadLicencia=Logica_Licencia.generarCaducidad(titularValidado, nuevaLicencia);
        
            nuevaLicencia.setFechaCaducidad(fechaCaducidadLicencia);
            
            licenciaEmitida=Logica.Logica_Licencia.emitirLicencia(titularValidado,nuevaLicencia);
            
            if(licenciaEmitida)
                JOptionPane.showMessageDialog(null, "Licencia generada. Tipo de nueva licencia: " + licenciaElegida + ". Fecha de caducidad: " + fechaCaducidadLicencia.toString() + ".");
        }
        else{
            JOptionPane.showMessageDialog(null, "El titular ya posee licencia del tipo seleccionado.");
        }
    }
    
    private void fieldFiller(Titular titularObtenido, Licencia[] licenciasObtenidas){ //Llena los campos de la interfaz cuando se presiona "Validar" después de ingresar un DNI.
     
        //Grupo sanguíneo y factor RH en hardcode.
        
        checkBoxLicenciaA.setEnabled(false);
        checkBoxLicenciaB.setEnabled(false);
        checkBoxLicenciaC.setEnabled(false);
        checkBoxLicenciaD.setEnabled(false);
        checkBoxLicenciaE.setEnabled(false);
        checkBoxLicenciaF.setEnabled(false);
        
        checkBoxLicenciaA.setSelected(false);
        checkBoxLicenciaB.setSelected(false);
        checkBoxLicenciaC.setSelected(false);
        checkBoxLicenciaD.setSelected(false);
        checkBoxLicenciaE.setSelected(false);
        checkBoxLicenciaF.setSelected(false);
        
        //Inicializamos los checkboxes que muestran las licencias vigentes.
        
        for(int i=0; i<licenciasObtenidas.length; i++){
            if((licenciasObtenidas[i].getTipoLicencia())!=null){
                if((licenciasObtenidas[i].getTipoLicencia()).equals("A"))
                    checkBoxLicenciaA.setSelected(true);
                if((licenciasObtenidas[i].getTipoLicencia()).equals("B"))
                    checkBoxLicenciaB.setSelected(true);
                if((licenciasObtenidas[i].getTipoLicencia()).equals("C"))
                    checkBoxLicenciaC.setSelected(true);
                if((licenciasObtenidas[i].getTipoLicencia()).equals("D"))
                    checkBoxLicenciaD.setSelected(true);
                if((licenciasObtenidas[i].getTipoLicencia()).equals("E"))
                    checkBoxLicenciaE.setSelected(true);
                if((licenciasObtenidas[i].getTipoLicencia()).equals("F"))
                    checkBoxLicenciaF.setSelected(true);
            }
            else
                break;
        }
        
        //Asignamos las variables de clase a la información traída desde la BD.
        
        nombreTitular=titularObtenido.getNombre();
        apellidoTitular=titularObtenido.getApellido();
        fechaNacimientoTitular=titularObtenido.getFechaNacimiento();
        observacionesTitular=titularObtenido.getObservaciones();
        
        
        //Mostramos en los textfields la información traída desde la BD.
        
        textFieldNombreTitular.setText(titularObtenido.getNombre());
        textFieldApellidoTitular.setText(titularObtenido.getApellido());
        textFieldGrupoSanguineoTitular.setText(titularObtenido.getGrupoSanguineo());
        textFieldFactorRHTitular.setText(titularObtenido.getFactorSanguineo());
        textFieldFechaNacimientoTitular.setText((titularObtenido.getFechaNacimiento()).toString());
        textAreaObservacionesTitular.setText(titularObtenido.getObservaciones());
        checkBoxDonaOrganos.setEnabled(false);
        
        if(titularObtenido.getDonanteOrganos().equals("true"))
            checkBoxDonaOrganos.setSelected(true);
        else
            checkBoxDonaOrganos.setSelected(true);
        
        

        
        Date edadNacimientoAux=titularObtenido.getFechaNacimiento();
        int year=edadNacimientoAux.getYear();
        int month=edadNacimientoAux.getMonth();
        int day=edadNacimientoAux.getDay();
        
        edadTitular=Logica_titular.obtenerEdad(titularObtenido);
        //JOptionPane.showMessageDialog(null, "Edad del Titular: " + edadTitular.toString() + " años.");
        
    }

    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel5 = new javax.swing.JPanel();
        jLabel20 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        textFieldDocumentoTitular = new javax.swing.JTextField();
        jLabel18 = new javax.swing.JLabel();
        buttonValidar = new javax.swing.JButton();
        checkBoxLicenciaA = new javax.swing.JCheckBox();
        checkBoxLicenciaB = new javax.swing.JCheckBox();
        checkBoxLicenciaC = new javax.swing.JCheckBox();
        checkBoxLicenciaD = new javax.swing.JCheckBox();
        checkBoxLicenciaE = new javax.swing.JCheckBox();
        checkBoxLicenciaF = new javax.swing.JCheckBox();
        textFieldNombreTitular = new java.awt.TextField();
        textFieldApellidoTitular = new java.awt.TextField();
        textFieldFechaNacimientoTitular = new java.awt.TextField();
        textFieldGrupoSanguineoTitular = new java.awt.TextField();
        textFieldFactorRHTitular = new java.awt.TextField();
        textAreaObservacionesTitular = new java.awt.TextArea();
        labelLicenciaAEmitir = new javax.swing.JLabel();
        comboBoxLicenciaAEmitir = new javax.swing.JComboBox();
        donaOrganosTextBox = new javax.swing.JLabel();
        checkBoxDonaOrganos = new javax.swing.JCheckBox();
        jPanel6 = new javax.swing.JPanel();
        jLabel21 = new javax.swing.JLabel();
        buttonEmitirLicencia = new javax.swing.JButton();
        volverEmitir = new javax.swing.JButton();

        jPanel5.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel20.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        jLabel20.setText("Dar de alta titular");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(jLabel20)
                .addContainerGap(40, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel20)
                .addContainerGap())
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos del titular", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 13))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        jLabel1.setText("Nombre:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        jLabel2.setText("Apellido:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        jLabel4.setText("Grupo sanguíneo:");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        jLabel3.setText("Fecha de nacimiento:");

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        jLabel7.setText("Factor RH:");

        jLabel19.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        jLabel19.setText("Observaciones:");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Ingresar"));

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        jLabel8.setText("Documento:");

        textFieldDocumentoTitular.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                textFieldDocumentoTitularKeyTyped(evt);
            }
        });

        jLabel18.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        jLabel18.setText("Licencias vigentes del titular:");

        buttonValidar.setText("Validar");
        buttonValidar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonValidarActionPerformed(evt);
            }
        });

        checkBoxLicenciaA.setText("A");

        checkBoxLicenciaB.setText("B");

        checkBoxLicenciaC.setText("C");

        checkBoxLicenciaD.setText("D");

        checkBoxLicenciaE.setText("E");

        checkBoxLicenciaF.setText("F");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(textFieldDocumentoTitular, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 164, Short.MAX_VALUE)
                                .addComponent(checkBoxLicenciaA))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(buttonValidar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(checkBoxLicenciaD)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(checkBoxLicenciaE)
                                .addGap(18, 18, 18)
                                .addComponent(checkBoxLicenciaF))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(checkBoxLicenciaB)
                                .addGap(18, 18, 18)
                                .addComponent(checkBoxLicenciaC)))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel18)
                        .addGap(52, 52, 52))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(13, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jLabel18))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textFieldDocumentoTitular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(checkBoxLicenciaA)
                    .addComponent(checkBoxLicenciaB)
                    .addComponent(checkBoxLicenciaC))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonValidar)
                    .addComponent(checkBoxLicenciaD)
                    .addComponent(checkBoxLicenciaE)
                    .addComponent(checkBoxLicenciaF))
                .addGap(4, 4, 4))
        );

        textFieldDocumentoTitular.getAccessibleContext().setAccessibleName("dniELicencia");
        buttonValidar.getAccessibleContext().setAccessibleName("validarELicencia");
        checkBoxLicenciaA.getAccessibleContext().setAccessibleName("aELicencia");
        checkBoxLicenciaB.getAccessibleContext().setAccessibleName("bELicencia");
        checkBoxLicenciaC.getAccessibleContext().setAccessibleName("cELicencia");
        checkBoxLicenciaD.getAccessibleContext().setAccessibleName("dELicencia");
        checkBoxLicenciaE.getAccessibleContext().setAccessibleName("eELicencia");
        checkBoxLicenciaF.getAccessibleContext().setAccessibleName("fELicencia");

        textFieldApellidoTitular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textFieldApellidoTitularActionPerformed(evt);
            }
        });

        textFieldGrupoSanguineoTitular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textFieldGrupoSanguineoTitularActionPerformed(evt);
            }
        });

        labelLicenciaAEmitir.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        labelLicenciaAEmitir.setText("Licencia a la que aspira el titular:");

        comboBoxLicenciaAEmitir.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "A", "B", "C", "D", "E", "F" }));
        comboBoxLicenciaAEmitir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboBoxLicenciaAEmitirActionPerformed(evt);
            }
        });

        donaOrganosTextBox.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        donaOrganosTextBox.setText("Dona órganos:");

        checkBoxDonaOrganos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkBoxDonaOrganosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(textAreaObservacionesTitular, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(labelLicenciaAEmitir)
                                .addGap(18, 18, 18)
                                .addComponent(comboBoxLicenciaAEmitir, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(jPanel2Layout.createSequentialGroup()
                                            .addComponent(jLabel2)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(textFieldApellidoTitular, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addGroup(jPanel2Layout.createSequentialGroup()
                                            .addComponent(jLabel1)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(textFieldNombreTitular, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addComponent(jLabel19))
                                .addGap(55, 55, 55)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(donaOrganosTextBox)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(checkBoxDonaOrganos))
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(textFieldFechaNacimientoTitular, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(jLabel4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(textFieldGrupoSanguineoTitular, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(textFieldFactorRHTitular, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addGap(0, 50, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(jLabel4)
                        .addComponent(jLabel7))
                    .addComponent(textFieldNombreTitular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textFieldGrupoSanguineoTitular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textFieldFactorRHTitular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel2)
                        .addComponent(textFieldApellidoTitular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel3)
                    .addComponent(textFieldFechaNacimientoTitular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel19)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(donaOrganosTextBox)
                        .addComponent(checkBoxDonaOrganos)))
                .addGap(13, 13, 13)
                .addComponent(textAreaObservacionesTitular, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelLicenciaAEmitir)
                    .addComponent(comboBoxLicenciaAEmitir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(38, Short.MAX_VALUE))
        );

        comboBoxLicenciaAEmitir.getAccessibleContext().setAccessibleDescription("");

        jPanel6.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel21.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        jLabel21.setText("Emitir Licencia");

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addComponent(jLabel21)
                .addContainerGap(37, Short.MAX_VALUE))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel21)
                .addContainerGap())
        );

        buttonEmitirLicencia.setText("Emitir Licencia");
        buttonEmitirLicencia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonEmitirLicenciaActionPerformed(evt);
            }
        });

        volverEmitir.setText("Volver");
        volverEmitir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                volverEmitirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(195, 195, 195)
                        .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(volverEmitir, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(buttonEmitirLicencia, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 75, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonEmitirLicencia)
                    .addComponent(volverEmitir))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void textFieldGrupoSanguineoTitularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textFieldGrupoSanguineoTitularActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_textFieldGrupoSanguineoTitularActionPerformed

    private void textFieldApellidoTitularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textFieldApellidoTitularActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_textFieldApellidoTitularActionPerformed

    private void buttonValidarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonValidarActionPerformed
        // TODO add your handling code here:
        
        for(int i=0; i<licenciasValidadas.length; i++)
            licenciasValidadas[i]=new Licencia();
        
        if(textFieldDocumentoTitular.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Ingrese un DNI válido.");
        }
        else{
            documentoTitular=Integer.parseInt(textFieldDocumentoTitular.getText());
            existeTitular=BD.ABM_Titular.obtenerTitular(titularValidado, documentoTitular);
            if(existeTitular){
                BD.ABM_Licencia.obtenerLicencias(licenciasValidadas, documentoTitular);
            }
            fieldFiller(titularValidado, licenciasValidadas);
        }
    }//GEN-LAST:event_buttonValidarActionPerformed

    private void textFieldDocumentoTitularKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textFieldDocumentoTitularKeyTyped
        if(textFieldDocumentoTitular.getText().length()>=8){
            evt.consume();
        }

        char Letra = evt.getKeyChar();
        if(!Character.isDigit(Letra)){
            evt.consume();
        }
    }//GEN-LAST:event_textFieldDocumentoTitularKeyTyped

    private void buttonEmitirLicenciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonEmitirLicenciaActionPerformed
        licenciaElegida=(String)comboBoxLicenciaAEmitir.getSelectedItem();
        
        if(!licenciaElegida.equals("") && existeTitular){
            EmitirNuevaLicencia(titularValidado);
            buttonValidarActionPerformed(evt);
        }
        else if(!existeTitular)
            JOptionPane.showMessageDialog(null, "Antes debe ingresarse un titular.");
    }//GEN-LAST:event_buttonEmitirLicenciaActionPerformed

    private void comboBoxLicenciaAEmitirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboBoxLicenciaAEmitirActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_comboBoxLicenciaAEmitirActionPerformed

    private void volverEmitirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_volverEmitirActionPerformed
        MenuOperador volver = new MenuOperador();
        volver.setVisible(true);
        dispose();
    }//GEN-LAST:event_volverEmitirActionPerformed

    private void checkBoxDonaOrganosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkBoxDonaOrganosActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_checkBoxDonaOrganosActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EmitirLicencia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EmitirLicencia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EmitirLicencia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EmitirLicencia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EmitirLicencia().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonEmitirLicencia;
    private javax.swing.JButton buttonValidar;
    private javax.swing.JCheckBox checkBoxDonaOrganos;
    private javax.swing.JCheckBox checkBoxLicenciaA;
    private javax.swing.JCheckBox checkBoxLicenciaB;
    private javax.swing.JCheckBox checkBoxLicenciaC;
    private javax.swing.JCheckBox checkBoxLicenciaD;
    private javax.swing.JCheckBox checkBoxLicenciaE;
    private javax.swing.JCheckBox checkBoxLicenciaF;
    private javax.swing.JComboBox comboBoxLicenciaAEmitir;
    private javax.swing.JLabel donaOrganosTextBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JLabel labelLicenciaAEmitir;
    private java.awt.TextArea textAreaObservacionesTitular;
    private java.awt.TextField textFieldApellidoTitular;
    private javax.swing.JTextField textFieldDocumentoTitular;
    private java.awt.TextField textFieldFactorRHTitular;
    private java.awt.TextField textFieldFechaNacimientoTitular;
    private java.awt.TextField textFieldGrupoSanguineoTitular;
    private java.awt.TextField textFieldNombreTitular;
    private javax.swing.JButton volverEmitir;
    // End of variables declaration//GEN-END:variables

}
